package com.controller;

import com.github.pagehelper.PageInfo;
import com.pojo.Role;
import com.service.RoleService;
import com.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class RoleController {
    @Autowired
    private RoleService roleService;



    /**
     * 根据id查询角色信息
     * @param id
     * @return
     */
    @RequestMapping("/selectRoleById")
    public Object selectRoleById(long id){
        R r=new R<Role>();
        Role role = roleService.selectRoleById(id);
        if(role!=null){
            r.setCode(200);
            r.setData(role);
        }else{
            r.setCode(400);
            r.setMessage("角色不存在");
        }
        return r;
    }

    /**
     * 查询所有角色信息
     * @param
     * @return
     */
    @RequestMapping("/selectAllRoles")
    public Object selectAllRoles(){
        R r=new R<Role>();
        List<Role> roles = roleService.selectAllRoles();
        if(roles!=null){
            r.setCode(200);
            r.setData(roles);
        }else{
            r.setCode(400);
            r.setMessage("角色不存在");
        }
        return r;
    }

    /**
     * 根据管理员id查询所有角色信息
     * @param
     * @return
     */
    @RequestMapping("/selectAllRoleById")
    public Object selectAllRoleById(long id){
        R r=new R<Role>();
        List<Role> roles = roleService.selectAllRoleById(id);
        if(roles!=null){
            r.setCode(200);
            r.setData(roles);
        }else{
            r.setCode(400);
            r.setMessage("角色不存在");
        }
        return r;
    }

    /**
     *name模糊查询 分页查询
     * @param pageNum
     * @param name
     * @return
     */
    @RequestMapping("/selectRolesByName")
    public Object selectRolesByName(@RequestParam(value = "pageNum",required = true,defaultValue= "1") int pageNum,@RequestParam(value = "name",required = true,defaultValue= "") String name  ){
        R r=new R<Role>();
        Map map=new HashMap();
        map.put("pageNum",pageNum);
        map.put("name",name);
        PageInfo pageInfo =roleService.selectRolesByName(map);
        r.setCode(200);
        r.setData(pageInfo);
        return r;
    }
    /**
     * 更新角色信息
     * @param role
     * @return
     */
    @RequestMapping("/updateRole")
    public Object updateRole(Role role){
        R r=new R<Role>();
        int i = roleService.updateRole(role);
        if(i!=0){
            r.setCode(200);
            r.setMessage("更新成功");
            r.setData(1);
        }else{
            r.setCode(400);
            r.setMessage("更新失败");
            r.setData(0);
        }
        return r;
    }


    /**
     * 插入角色
     * @param role
     * @return
     */
    @RequestMapping("/insertRole")
    public Object insertRole(Role role){
        R r=new R<Role>();
        int i = roleService.insertRole(role);
        if(i!=0){
            r.setCode(200);
            r.setMessage("插入成功");
            r.setData(1);
        }else{
            r.setCode(400);
            r.setMessage("插入失败");
            r.setData(0);
        }
        return r;
    }

    /**
     * 根据id删除角色
     * @param id
     * @return
     */
    @RequestMapping("/deleteRoleById")
    public Object deleteRoleById(long id){
        R r=new R<Role>();
        int i = roleService.deleteRoleById(id);
        if(i!=0){
            r.setCode(200);
            r.setMessage("删除成功");
            r.setData(1);
        }else{
            r.setCode(400);
            r.setMessage("删除失败");
            r.setData(0);
        }
        return r;
    }


    /**
     * 根据id恢复角色
     * @param id
     * @return
     */
    @RequestMapping("/resumeRoleById")
    public Object resumeRoleById(long id){
        R r=new R<Role>();
        int i = roleService.resumeRoleById(id);
        if(i!=0){
            r.setCode(200);
            r.setMessage("恢复成功");
            r.setData(1);
        }else{
            r.setCode(400);
            r.setMessage("恢复失败");
            r.setData(0);
        }
        return r;
    }

    /**
     * 为角色分权
     * @param rid
     * @param pids
     * @return
     */
    @RequestMapping("/updateRolePower")
    public Object updateRolePower(long rid,long[] pids){
        R r=new R<Role>();
        int i = roleService.updateRolePower(rid,pids);
        if(i!=0){
            r.setCode(200);
            r.setMessage("插入成功");
            r.setData(1);
        }else{
            r.setCode(400);
            r.setMessage("插入失败");
            r.setData(0);
        }
        return r;
    }
}
